package com.heima.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.crm.common.result.PageResult;
import com.heima.crm.common.result.Result;
import com.heima.crm.mapper.SysNoticeMapper;
import com.heima.crm.pojos.dtos.NoticePageDTO;
import com.heima.crm.pojos.entity.SysNotice;
import com.heima.crm.pojos.vos.NoticeVO;
import com.heima.crm.service.SysNoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysNoticeServiceImpl implements SysNoticeService {
    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    @Override
    public Result findList(NoticePageDTO dto) {
        IPage page=new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<SysNotice>()
                .eq(SysNotice::getStatus, dto.getStatus());
        page = sysNoticeMapper.selectPage(page, wrapper);
        List<SysNotice> records = page.getRecords();
        if (records==null || records.size()==0){
            return Result.success();
        }
        ArrayList<NoticeVO> list = new ArrayList<>();
        for (SysNotice record : records) {
            NoticeVO vo = new NoticeVO();
            BeanUtils.copyProperties(record,vo);
            list.add(vo);
        }


        return Result.success(new PageResult<>(page.getTotal(),list));
    }
}
